package com.peng.leetcode.slidingWindow;

/**
 * LongestOnes
 * <p>
 * 1004. 最大连续1的个数 III
 *
 * @author: lupeng6
 * @create: 2021/2/19 10:05
 */
public class LongestOnes {

    public static void main(String[] args) {
        int[] nums = {0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1};
        int k = 3;
        System.out.println(new LongestOnes().longestOnes(nums, k));
    }

    public int longestOnes(int[] A, int K) {
        int left = 0;
        int right = 0;
        int zeroSize = 0;

        int answer = 0;

        int length = A.length;
        while (right < length) {
            if (A[right++] == 0) {
                zeroSize++;
            }

            // 窗口内的0数量超过指定数
            while (zeroSize > K) {
                if (A[left++] == 0) {
                    zeroSize--;
                }
            }

            // 计算窗口大小
            answer = Math.max(answer, right - left);
        }

        return answer;
    }
}
